<template>
  <!-- 筛选区 -->
  <div v-if="filterData && !filterLoading" class="sub-filter">
    <div class="item">
      <div class="head">品牌:</div>
      <div class="body">
        <a
          v-for="brand in filterData.brands"
          :key="brand.id"
          href="javascript"
          :class="{ active: filterData.selectedBrand === brand.id }"
          @click="changeBrand(brand.id)"
          >{{ brand.name }}</a
        >
      </div>
    </div>
    <div v-for="p in filterData.saleProperties" :key="p.id" class="item">
      <div class="head">{{ p.name }}:</div>
      <div class="body">
        <a
          v-for="attr in p.properties"
          :key="attr.id"
          :class="{ active: p.selectedProp === attr.id }"
          href="javascript:;"
          @click="changeAttr(p, attr.id)"
          >{{ attr.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <RbtSkeleton class="item" width="800px" height="40px" />
    <RbtSkeleton class="item" width="800px" height="40px" />
    <RbtSkeleton class="item" width="800px" height="40px" />
    <RbtSkeleton class="item" width="800px" height="40px" />
    <RbtSkeleton class="item" width="800px" height="40px" />
  </div>
</template>

<script setup lang="ts">
import { FilterType, AttrType, NameType } from '@/types/category'
import { findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'

const route = useRoute()
const filterData = ref<FilterType>()
const filterLoading = ref<boolean>(false)
const emits = defineEmits<{
  (e: 'filter-change', params: AttrType): void
}>()

// 分类变化的时候需要重新获取筛选数据 使用侦听器
watch(
  () => route.params.id,
  newVal => {
    // 当从二级分类去顶级分类也会拿到id 不能去加载数据 因为不是二级分类的id
    if (newVal && route.path === `/category/sub/${newVal}`) {
      filterLoading.value = true
      newVal &&
        findSubCategoryFilter(route.params.id as string).then(res => {
          // 品牌全部
          res.data.result.selectedBrand = null
          res.data.result.brands.unshift({ id: null, name: '全部' })
          // 销售属性全部
          res.data.result.saleProperties.forEach(
            (p: {
              selectedProp: any
              properties: { id: any; name: string }[]
            }) => {
              p.selectedProp = undefined
              p.properties.unshift({ id: null, name: '全部' })
            }
          )
          filterData.value = res.data.result
          filterLoading.value = false
        })
    }
  },
  { immediate: true }
)

// 获取筛选参数
const getFilterParams = (): AttrType => {
  const filterParams: any = {}
  const attrs: AttrType[] = []
  filterParams.brandId = filterData.value.selectedBrand
  filterData.value.saleProperties.forEach(p => {
    const attr = p.properties.find(attr => attr.id === p.selectedProp)
    if (attr && attr.id !== undefined) {
      attrs.push({
        groupName: p.name,
        propertyName: attr.name
      })
    }
  })
  if (attrs.length) filterParams.attrs = attrs
  return filterParams
}

// 选择品牌
const changeBrand = (brandId: string) => {
  if (filterData.value.selectedBrand === brandId) return
  filterData.value.selectedBrand = brandId
  emits('filter-change', getFilterParams())
}

// 选择属性
const changeAttr = (p: NameType, attrId: string) => {
  if (p.selectedProp === attrId) return
  p.selectedProp = attrId
  emits('filter-change', getFilterParams())
}
</script>

<style scoped lang="scss">
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: $rbtColor;
        }
      }
    }
  }
}
</style>
